package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.SysRoleDto;
import com.itheima.ydd.entity.PageBean;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.entity.SysRole;
import com.itheima.ydd.entity.SysRoleMenu;
import com.itheima.ydd.service.ISysMenuService;
import com.itheima.ydd.service.ISysRoleMenuService;
import com.itheima.ydd.service.ISysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/sys_role")
@Slf4j
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ISysRoleMenuService sysRoleMenuService;
    @Autowired
    private ISysMenuService sysMenuService;



    /**
     * 添加角色
     * @param token
     * @param sysRoleDto
     * @return
     */
    @PostMapping("/add")
    public ResultData save(@RequestHeader("x-token") String token, @RequestBody SysRoleDto sysRoleDto){
        log.info("{}",sysRoleDto);
        //设置对应的时间戳(已经改为自动注入)
        //sysRoleDto.setCreateDate(LocalDateTime.now());
        //sysRoleDto.setUpdateDate(LocalDateTime.now());
        sysRoleService.saveWithMenu(sysRoleDto);
        return ResultData.ok("添加成功", " ");
    }

    /**
     * 权限详情页面
     * @param token
     * @param
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(@RequestHeader("x-token") String token,String id) {
        log.info("{}", id);
        SysRoleDto sysRoleDto = sysRoleService.getSysRoleInfo(id);
        return ResultData.ok("", sysRoleDto);
    }

    /**
     * 修改角色
     * @param token
     * @param sysRoleDto
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestHeader("x-token") String token, @RequestBody SysRoleDto sysRoleDto){
        log.info("SysRoleDto",sysRoleDto);
        //调用修改方法
        sysRoleService.updateWithMenu(sysRoleDto);
        System.out.println(sysRoleDto);
        return ResultData.ok(" ", " ");
    }

    /**
     *删除
     * @param token
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(@RequestHeader("x-token") String token, String id) {
        System.out.println(id);
        sysRoleService.delWithMenu(id);
        return ResultData.ok(" ", " ");
    }



    /*********
     * 用户添加角色信息回填
     * @return
     */
    @GetMapping("/allData")
    public ResultData allData(){
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(SysRole::getCreateDate);
        List<SysRole> sysRoles = sysRoleService.list(queryWrapper);
        return ResultData.ok("查询成功",sysRoles);
    }

    @GetMapping("/list")
    public ResultData list(String name,int page,int limit){
        Page<SysRole> sysRolePage = new Page<>(page,limit);
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),SysRole::getName,name);
        sysRoleService.page(sysRolePage,queryWrapper);

        List<SysRole> records = sysRolePage.getRecords();
        List<SysRole> sysRoles = records.stream().map(sysRole -> {
            String roleId = sysRole.getId();
            //查询sysRoles
            LambdaQueryWrapper<SysRoleMenu> srmQueryWrapper = new LambdaQueryWrapper<>();
            srmQueryWrapper.eq(SysRoleMenu::getRoleId, roleId);
            List<SysRoleMenu> sysRoleMenus = sysRoleMenuService.list(srmQueryWrapper);
            sysRole.setRole_id(roleId);
            //查询menuID集合
            List<String> collect = sysRoleMenus.stream().map(sysRoleMenu -> {
                return sysRoleMenu.getMenuId();
            }).collect(Collectors.toList());
            sysRole.setRole_menus(collect);
            return sysRole;
        }).collect(Collectors.toList());
        PageBean<SysRole> sysRolePageBean = new PageBean<>();
        sysRolePageBean.setCount((int) sysRolePage.getTotal());
        sysRolePageBean.setCurrentPage((int) sysRolePage.getCurrent());
        sysRolePageBean.setPageSize((int) sysRolePage.getSize());
        sysRolePageBean.setTotalPages((int) sysRolePage.getPages());
        sysRolePageBean.setData(sysRoles);
        return ResultData.ok("分页查询成功",sysRolePageBean);
    }


}
